package com.cddx.logistics.controller;

import com.cddx.logistics.domain.dto.QueryDTO;
import com.cddx.logistics.domain.dto.RoleMenuDTO;
import com.cddx.logistics.domain.pojo.Role;
import com.cddx.logistics.service.RoleService;
import com.cddx.logistics.util.model.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {
    @Resource
    private RoleService roleService;

    @PostMapping("/add")
    @ApiOperation("添加角色")
    public R addRole(@RequestBody Role role){
        return roleService.addRole(role)?R.ok():R.error();
    }

    @PostMapping("/update")
    @ApiOperation("修改角色")
    public R updateRole(@RequestBody Role role){
        return roleService.updateRole(role)?R.ok():R.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除角色")
    public R deleteRole(@PathVariable("id") Integer id){
        return roleService.deleteRole(id)?R.ok():R.error();
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询角色")
    public R queryPage(@RequestBody QueryDTO  role){
        return R.ok(roleService.queryPage(role));
    }
    @GetMapping("/queryAll")
    @ApiOperation("查询所有角色 渲染下拉列表")
    public R queryAll(){
        return R.ok(roleService.queryAll());
    }
    @PostMapping("/deleteIds")
    @ApiOperation("批量删除角色")
    public R deleteIds(@RequestBody Integer[] ids){
        return roleService.deleteIds(ids)?R.ok():R.error("删除失败");
    }
    @GetMapping("/queryMenu/{id}")
    @ApiOperation("查询角色已授权的菜单")
    public R queryMenu(@PathVariable("id") Integer id){
        return R.ok(roleService.queryMenu(id));
    }

    @PostMapping("/saveMenuAuth")
    @ApiOperation("保存角色菜单权限")
    public R saveMenuAuth(@RequestBody RoleMenuDTO roleMenuDTO){
        return roleService.addMenuAuth(roleMenuDTO)?R.ok():R.error("添加失败");
    }

}
